package com.chinairi.mall.controller;


import com.chinairi.common.utils.RedisUtil;
import com.chinairi.common.utils.ResponseUtil;
import com.chinairi.mall.controller.viewobject.UserVO;
import com.chinairi.mall.model.SysMenu;
import com.chinairi.mall.service.ISysUserLoginService;
import com.chinairi.mall.service.modelobject.CiccLabelMO;
import com.chinairi.mall.service.modelobject.SysUserLoginMO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import com.chinairi.common.controller.BaseController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户账号信息表 前端控制器
 * </p>
 *
 * @author 董志涵
 * @since 2020-08-17
 */
@RestController
@RequestMapping("/sys-user-login")
public class SysUserLoginController extends BaseController {
    @Autowired
    private ISysUserLoginService iSysUserLoginService;
    @Autowired
    private RedisUtil redisUtil;

    /**
     * 登录
     *
     * @param response
     * @param request
     * @throws Exception
     */
    @RequestMapping("/login")
    public void login(HttpServletResponse response, HttpServletRequest request) throws Exception {
        SysUserLoginMO sysUserLoginMO = iSysUserLoginService.login(response, request);
        ResponseUtil.showMessage("success", "登录信息", sysUserLoginMO);
    }

    /**
     * 登出
     *
     * @param response
     * @param request
     * @throws Exception
     */
    @PostMapping("logout")
    public void logout(HttpServletResponse response, HttpServletRequest request) throws Exception {
        String token = "";
        token = request.getHeader("token");
        redisUtil.del(token);
        ResponseUtil.showMessage("success", "登出", "登出成功");
    }

    /**
     * 查看邮箱是否可以注册
     *
     * @param userMail
     * @throws IOException
     */
    @RequestMapping("/getSysUserByUserMail")
    public void getSysUserByUserMail(@RequestParam(name = "userMail") String userMail) throws IOException {
        if (iSysUserLoginService.getSysUserByUserMail(userMail) != null) {
            ResponseUtil.showMessage("fail", "注册信息", "该邮箱已注册");
        } else {
            ResponseUtil.showMessage("success", "注册信息", "邮箱可以注册");
        }
    }

    /**
     * 查看用户名是否可以注册
     *
     * @param userName
     * @throws IOException
     */
    @RequestMapping("/getSysUserByUserName")
    public void getSysUserByUserName(@RequestParam(name = "userName") String userName) throws IOException {
        if (iSysUserLoginService.getSysUserByUserName(userName) != null) {
            ResponseUtil.showMessage("fail", "注册信息", "该用户名已经注册");
        } else {
            ResponseUtil.showMessage("success", "注册信息", "用户名可以注册");
        }
    }

    /**
     * 在用户中心修改密码
     *
     * @param request
     * @throws Exception
     */
    @PutMapping("/changePassword")
    public void changePassword(HttpServletRequest request) throws Exception {
        iSysUserLoginService.changePassword(request);
        ResponseUtil.showMessage("success", "密码", "修改成功");
    }

    /**
     * 强制修改密码
     *
     * @param request
     * @throws Exception
     */
    @RequestMapping("/forceChangePassword")
    public void forceChangePassword(HttpServletRequest request) throws Exception {
        iSysUserLoginService.forceChangePassword(request);
        ResponseUtil.showMessage("success", "密码", "修改成功");
    }

    /**
     * 反转手机号验证码绑定
     *
     * @param phoneNumber
     * @param request
     */
    @GetMapping("/checkPhoneNumberEx")
    public void checkPhoneNumberEx(String phoneNumber, HttpServletRequest request) throws Exception {
        ResponseUtil.showMessage("success", "验证码已发送", iSysUserLoginService.checkPhoneNumberEx(phoneNumber, request));
    }

    /**
     * 忘记密码
     *
     * @param request
     * @throws Exception
     */
    @PutMapping("/forgetPassword")
    public void forgetPassword(String phone, HttpServletRequest request) throws Exception {
        iSysUserLoginService.forgetPassword(phone, request);
        ResponseUtil.showMessage("success", "密码", "修改成功");
    }

    /**
     * 获取邮箱验证码
     * @param mail
     * @param request
     * @throws IOException //@MyLog(value = "给用户绑定otp验证码")  //这里添加了AOP的自定义注解
     */
    @GetMapping("/checkMail")
    @ResponseBody
    public void checkMail(String mail, HttpServletRequest request) throws IOException {
        ResponseUtil.showMessage("success", "获取邮箱验证码成功", iSysUserLoginService.checkMail(mail,request));
    }

    /**
     * @param mail
     * @param request
     * @throws IOException //@MyLog(value = "给用户绑定otp验证码")  //这里添加了AOP的自定义注解
     */
    @GetMapping("/updateMail")
    @ResponseBody
    public void updateMail(String mail, HttpServletRequest request) throws IOException {
        iSysUserLoginService.updateMail(mail, request);
        ResponseUtil.showMessage("success", "邮箱绑定成功", null);
    }

    /**
     * 校验密码
     * @param request
     * @throws Exception
     */
    @GetMapping("/passwordCheck")
    @ResponseBody
    public void passwordCheck(HttpServletRequest request) throws Exception {
        if (iSysUserLoginService.passwordCheck(request)){
            ResponseUtil.showMessage("success", "密码验证成功", null);
        }
    }

    /**
     * 获取菜单信息
     * @param menuId 路由地址
     * @return 菜单信息
     */
    @GetMapping("listPCWebMenu")
    public List<SysMenu> listPCWebMenu(String menuId) {
        return iSysUserLoginService.listPCWebMenu(menuId);
    }

    /**
     * 从redis中获取地区map
     * @throws Exception 异常
     */
    @GetMapping("/getAreaMapFromRedis")
    public void test() throws Exception {
        Map<Object, Object> areaMap = iSysUserLoginService.getAreaMapFromRedis();
        ResponseUtil.showMessage("success", "获取地区编码表成功", areaMap);
    }

}
